package cn.stylefeng.guns.modular.business.frequency.ordercarverify.controller;

import cn.stylefeng.guns.core.context.login.LoginContextHolder;
import cn.stylefeng.guns.core.enums.ReturnCode;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.modular.business.custom.annotations.RepeatSubmit;
import cn.stylefeng.guns.modular.business.frequency.ordercarverify.entity.OrderCarVerify;
import cn.stylefeng.guns.modular.business.frequency.ordercarverify.model.param.OrderReturnParam;
import cn.stylefeng.guns.modular.business.frequency.orderlogistics.model.param.ErrorMsg;
import cn.stylefeng.guns.modular.business.frequency.ordersub.model.result.PkgOrderCarVerify;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.stylefeng.guns.modular.business.frequency.ordercarverify.service.OrderCarVerifyService;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 验车记录 控制器
 *
 * @author: yujie
 * @date: 2023/05/09 17:16
 */
@Api(tags = "验车信息")
@RestController
@RequestMapping("/api/verify")
public class OrderCarVerifyController {

    @Resource
    private OrderCarVerifyService orderCarVerifyService;

    //@Log("验车提交")
    //@NoRepeatSubmit
    @ApiOperation(value = "验车提交")
    @PostMapping
    @PreAuthorize("@el.check('verifycar:add')")
    public ResponseData addVerify(@Validated @RequestBody OrderCarVerify orderCarVerify) {
        StringBuilder errorMsg = new StringBuilder();
        if (!orderCarVerifyService.checkVerify(orderCarVerify, errorMsg)) {
            return ResponseData.andaError(errorMsg.toString());
        }
        Long userId = LoginContextHolder.me().getSysLoginUserId();
        orderCarVerifyService.addVerify(userId, orderCarVerify, true);
        return ResponseData.andaSuccess();
    }


    @ApiOperation(value = "回退验车")
    @PostMapping("/returnAddVerify")
    public ResponseData returnAddVerify( @RequestBody OrderReturnParam orderReturnParam) {
        if(orderReturnParam.getOrderIdList()!=null&&orderReturnParam.getOrderIdList().size()>0){
            for (Long orderId:orderReturnParam.getOrderIdList()) {
                orderCarVerifyService.returnAddVerify(orderId);
            }
        }
        return ResponseData.andaSuccess();
    }

    //@Log("验车编辑")
    @RepeatSubmit
    @ApiOperation(value = "验车编辑")
    @PutMapping
    @PreAuthorize("@el.check('verifycar:add')")
    public ResponseData updateCarVerify(@Validated @RequestBody OrderCarVerify orderCarVerify) {
        ErrorMsg errorMsg = new ErrorMsg();
       /* if (!orderCarVerifyService.checkVerifyUpdate(orderCarVerify, errorMsg)) {
            return ResponseData.andaError(errorMsg.getErrorMsg());
        }
        orderCarVerifyService.updateVerify(orderCarVerify);*/
        return ResponseData.success();
    }

    @ApiOperation(value = "子订单验车信息")
    @GetMapping("/{orderId}")
    public ResponseData getVerifyInfo(@PathVariable Long orderId) {
        PkgOrderCarVerify verify = orderCarVerifyService.getVerifyInfo(orderId);
        return ResponseData.andaSuccess(verify);
    }

    @ApiOperation(value = "批量获取验车信息")
    @GetMapping("/multi")
    public ResponseData getMultiVerifyInfo(@RequestParam("orderIds") Long[] orderIds) {
        List<Long> orderIdList = Arrays.asList(orderIds);
        List<PkgOrderCarVerify> list = orderCarVerifyService.getMultiVerifyInfo(orderIdList);
        return ResponseData.andaSuccess(list);
    }

    @ApiOperation(value = "验车撤销")
    @PutMapping("/repeal/{orderId}")
    public ResponseData repealVerifyCar(@PathVariable Long orderId) {
        ErrorMsg errorMsg = new ErrorMsg();
        if (!orderCarVerifyService.checkRepealVerifyCar(orderId, errorMsg)) {
            return ResponseData.andaError(ReturnCode.RETURN_PARAM_ERROR.code, errorMsg.getErrorMsg());
        }
        orderCarVerifyService.repealVerifyCar(orderId);
        return ResponseData.success();
    }



}
